package com.ruoyi.web.controller.module.product;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.product.ProductCombination;
import com.ruoyi.system.service.module.product.IProductCombinationService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 拼团产品Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "拼团产品控制器", tags = {"拼团产品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/product/combination")
public class ProductCombinationController extends BaseController {

    private final IProductCombinationService productCombinationService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询拼团产品列表
     */
    @ApiOperation("查询拼团产品列表")
    @PreAuthorize("@ss.hasPermi('module:combination:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductCombination> list(@Validated ProductCombination productCombination) {
        return PageUtils.buildDataInfo(productCombinationService.page(PageUtils.buildPage()));
    }

    /**
     * 导出拼团产品列表
     */
    @ApiOperation("导出拼团产品列表")
    @PreAuthorize("@ss.hasPermi('module:combination:export')")
    @Log(title = "拼团产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ProductCombination> export(@Validated ProductCombination productCombination) {
        List<ProductCombination> list = productCombinationService.list();
        ExcelUtil<ProductCombination> util = new ExcelUtil<>(ProductCombination.class);
        return util.exportExcel(list, "拼团产品");
    }

    /**
     * 获取拼团产品详细信息
     */
    @ApiOperation("获取拼团产品详细信息")
    @PreAuthorize("@ss.hasPermi('module:combination:query')")
    @GetMapping("/{id}")
    public AjaxResult<ProductCombination> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(productCombinationService.getById(id));
    }

    /**
     * 新增拼团产品
     */
    @ApiOperation("新增拼团产品")
    @PreAuthorize("@ss.hasPermi('module:combination:add')")
    @Log(title = "拼团产品", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ProductCombination productCombination) {
        return toAjax(productCombinationService.save(productCombination) ? 1 : 0);
    }

    /**
     * 修改拼团产品
     */
    @ApiOperation("修改拼团产品")
    @PreAuthorize("@ss.hasPermi('module:combination:edit')")
    @Log(title = "拼团产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCombination productCombination) {
        return toAjax(productCombinationService.updateById(productCombination) ? 1 : 0);
    }

    /**
     * 删除拼团产品
     */
    @ApiOperation("删除拼团产品")
    @PreAuthorize("@ss.hasPermi('module:combination:remove')")
    @Log(title = "拼团产品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(productCombinationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
